/*
 * NormalFitness.java
 *
 * Created on den 30 mars 2007, 23:30
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package grex.fitnessfunctions.Classification;

import grex.GP;
import grex.Options;
import grex.Prediction;
import grex.fitnessfunctions.FitnessFunction;
import grex.fitnessfunctions.IFitnessFunction;
import grex.genes.GeneException;

import java.io.Serializable;

/**
 *
 * @author rik
 */
public class BRIFitness extends FitnessFunction implements IFitnessFunction, Serializable {

    /** Creates a new instance of NormalFitness */
    public BRIFitness() {
        super(FitnessFunction.BRI);
    }

    @Override
    protected double calcPredictionError(Prediction prediction, double targetValue) {
            
            double[] probs = prediction.getProbs();
            double bri = 0;
            double E = 0;
            for (int i = 0; i < probs.length; i++) {                
                E = targetValue ==i ? 1 : 0;
                bri += Math.pow(probs[i] - E, 2);
            }
            return bri;        
    }
    
        /*public double calcFitness(GP gp) throws GeneException {
        Options options = gp.getOptions();
        gp.train();
        double trainFitness = 0;
        int E = 0;
        for (Prediction p : gp.getPcTrain().values()) {
            double[] probs = p.getProbs();
            double bri = 0;
            for (int i = 0; i < probs.length; i++) {                
                E = p.getTargetValue() ==i ? 1 : 0;
                bri += Math.pow(probs[i] - E, 2);
         //       System.out.println(probs[i]+ "\t" + E);
            }
         //   System.out.println("");
            trainFitness+=bri;
        }
        trainFitness = trainFitness*100 + trainFitness*100* options.getPUNISHMENT_FOR_LENGTH()*gp.getLength();
        if(gp.getLength()>options.getMAX_TREE_SIZE())
            trainFitness += gp.getLength();
        return trainFitness;
       
    }//*/
}
